package top.v5it.japi.plus.core.spring.web.interceptor;

import cn.hutool.core.collection.CollUtil;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.web.servlet.HandlerInterceptor;
import top.v5it.japi.plus.common.util.ServiceDiscoverUtil;

import java.util.List;

/**
 * 注入自定义的拦截器
 *
 * @author zhanpu
 * @date 2022/6/15
 */
public class JapiInterceptorConfigurationSelector implements ImportSelector {

    /**
     * 当前应用程序需要注入的类路径
     */
    private final List<String> LOAD_CLASS_LIST;

    /**
     * 构造函数加载需要注入的类路径
     */
    public JapiInterceptorConfigurationSelector() {
        LOAD_CLASS_LIST =
                ServiceDiscoverUtil.loadFactoryNames(HandlerInterceptor.class, this.getClass().getClassLoader());
    }

    /**
     * 导入拦截器
     *
     * @param ignore {@link AnnotationMetadata}
     * @return 类的全限名称
     */
    @Override
    public String[] selectImports(AnnotationMetadata ignore) {
        return CollUtil.isEmpty(LOAD_CLASS_LIST) ?
                new String[0]
                : LOAD_CLASS_LIST.toArray(new String[LOAD_CLASS_LIST.size()]);
    }
}
